<?php
class userGraphComponent extends sfComponent
{

  public function execute($request)
  {
    $c = aluCriteria::buildFromRequest($this->getRequest());
    $c->setPrimaryTableName(LogPeer::TABLE_NAME);
    $c->addAsColumn('nb', 'COUNT(*)');
    $c->addSelectColumn(LogPeer::UTILISATEUR);
    $c->addSelectColumn(LogPeer::TYPE);
    $c->addGroupByColumn(LogPeer::TYPE);
    $c->addGroupByColumn(LogPeer::UTILISATEUR);
    $stmt = BasePeer::doSelect($c);

    $connexions   = array();
    $deconnexions = array();
    foreach ($stmt as $lr)
    {
      $userId = $lr['UTILISATEUR'];
      array_key_exists($userId, $connexions) || $connexions[$userId] = 0;
      array_key_exists($userId, $deconnexions) || $deconnexions[$userId] = 0;

      if ($lr['TYPE'] == TypesPeer::TYPE_CONNEXION)
      {
        $connexions[$userId] = $lr['nb'];
      }
      elseif ($lr['TYPE'] == TypesPeer::TYPE_DECONNEXION)
      {
        $deconnexions[$userId] = $lr['nb'];
      }
    }
    $this->seriesX = array_keys($connexions);

    $this->series = array(
      array(
        'title'  => 'connexions',
        'data'   => array_values($connexions),
      ),
      array(
        'title'  => 'déconnexions',
        'data'   => array_values($deconnexions),
      ),
    );
  }
}